//
//  JOHomeViewController.m
//  新浪微博模拟
//
//  Created by song on 15/3/24.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOHomeViewController.h"
#import "JOStatus.h"
#import "JOUser.h"
#import "JOStatusFrame.h"
#import "JOStatusCell.h"
#import "AFNetworking.h"
#import "JOAccount.h"
#import "MJExtension.h"
@interface JOHomeViewController ()


@property(nonatomic , strong) NSMutableArray *statusFrame;

@end

@implementation JOHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn= [[UIButton alloc] init];
    [btn setTitle:@"首页" forState:UIControlStateNormal];
    btn.bounds = CGRectMake(0,0, 20, 30);
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(popview) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setTitleView:btn];
    
    
    self.statusFrame =[NSMutableArray array];
    [self testExt];
}

-(void)testExt
{
    NSMutableArray *array =[NSMutableArray array];
    
    for(int i=0 ;i<5 ;i++)
    {
        JOStatus *status =[[JOStatus alloc] init];
        status.idstr=@"123";
        status.text=@"hello word";
        status.source =@"good";
        
        [array addObject:status];
    }
    
    NSArray *s =[JOStatus objectArrayWithKeyValuesArray:array];
    for(JOStatus *s in array)
    {
        NSLog(@"%@",s);
    }
    
    
}


-(void)setStatusData
{
    
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filepath =[path stringByAppendingString:@"account.data"];
    
    JOAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
    
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"access_token"]=account.access_token;
    param[@"source"]=@"7ee8845d26b6ed60dad91d916923c29f";
    param[@"count"]=@20;
    param[@"page"]=@1;
    param[@"base_app"]=@0;
    [mgr GET:@"https://api.weibo.com/2/statuses/public_timeline.json" parameters:param success:^(AFHTTPRequestOperation * operation, id responseobj){
        NSArray *array = responseobj[@"statuses"];
        //        NSLog(@"请求成功  ---%@",responseobj);
        NSArray *statuses = [JOStatus objectArrayWithKeyValuesArray:array];
        for(int i=0 ;i<statuses.count ;i++)
        {
            JOStatusFrame *frame =[[JOStatusFrame alloc] init];
            frame.status = statuses[i];
            [self.statusFrame addObject:frame];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        NSLog(@"请求失败  ---%@",error);
    }];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.statusFrame.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JOStatusCell *cell = [JOStatusCell cellWithTableView:tableView];
    cell.statusFrame = self.statusFrame[indexPath.row];
    return cell;
}


//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UIViewController *vc = [[UIViewController alloc] init];
//    vc.view.backgroundColor = [UIColor redColor];
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
//}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JOStatusFrame *frame =self.statusFrame[indexPath.row];
    return  frame.cellHeight;
}

@end
